<?php

function getShoutboxMessages($iType = 0, $bNoAjax = false)
{
	if (!$bNoAjax) 
	{
		$oRespX = new xajaxResponse();
	}
	$oDb =& Database::get();
	$aRows = $oDb->getRows("
		SELECT s.text, s.time, u.user FROM
		". App::getT('shoutbox') ." AS s
			LEFT JOIN ". App::getT('user') ." AS u ON(u.id = s.user_id)
		WHERE s.type = '". $iType ."'
		ORDER BY s.time DESC LIMIT 0,20
	");

	$sTxt = '';
    foreach ($aRows as $aMsg)
    {
		$sTxt .= '<div style="padding:2px;">';
		$sTxt .= '<span style="font-size:7pt;">['.date('H:i', $aMsg['time']).'] </span>';
		$sTxt .= '<b><a href="'.App::makeUrl('public.user', array('name'=>$aMsg['user'])).'">'.$aMsg['user'].'</a>: </b>';
        $sTxt .= '<span>'. $aMsg['text'] .'</span>';
        $sTxt .= '</div>';
	}

	if ($bNoAjax) 
	{
		return $sTxt;		
	}
	else
	{
		$oRespX->addAssign('shoutbox_data', 'innerHTML', $sTxt);
		$oRespX->addScript('setTimeout("xajax_getShoutboxMessages('. $iType .')",4000);');
		return $oRespX;
	}
}

function addShoutOut($sText, $iType)
{
	$oRespX = new xajaxResponse();
	$oDb =& Database::get();
	
	$iLastTime = $oDb->getField('SELECT `time` FROM '.App::getT('shoutbox').' WHERE `user_id` = "'. App::getUser('id') .'" ORDER BY `time` DESC LIMIT 0,1');
	if((time()-$iLastTime) < App::getSetting('shoutbox_flood'))
    {
		$oRespX->addAlert(App::format('Mod_Site.flood_hold', App::getSetting('shoutbox_flood')));
    	return $oRespX;  	
    }
	define('PHPFOX_PROTECT_IMG', true);

	App::loadClass('Text');
    $oTxt = new Text();

    $sText = str_replace(">",  "&gt;", $sText);
	$sText = str_replace("<",  "&lt;", $sText);
    $sText = $oTxt->preParseChat($sText);

	if ( empty($sText) )
	{
		return $oRespX;
	}
	
	$sText = $oTxt->_splitString($sText, App::getParam('shoutbox_wrap'), "<br />\n");
    $sText = $oTxt->parsePostChat($sText);
    $sText = $oTxt->utf8ToUnicode($sText);
 
	$sTxt = '<div style="padding:2px;">';
	$sTxt .= '<span style="font-size:7pt;">['.date('H:i', time()).'] </span>';
	$sTxt .= '<b><a href="'.App::makeUrl('public.user', array('name'=>App::getUser('user'))).'">'.App::getUser('user').'</a>: </b>';
	$sTxt .= '<span>'. $sText .'</span>';
	$sTxt .= '</div>';
	$oRespX->addScript("document.getElementById('shoutbox_data').innerHTML = '". str_replace("'", "\'", $sTxt) ."' + document.getElementById('shoutbox_data').innerHTML; sId('shoutbox_div').scrollTop = 0; sId('shoutbox_text').focus();");

	$oDb->insert(App::getT('shoutbox'), array('user_id' => App::getUser('id'), 'text' => $sText, 'time' => time(), 'type' => $iType));

	return $oRespX;
}

?>